package com.androidvizlab.bachelor.Gui;

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.event.HyperlinkListener;

/**
 * Displays information about the application using html files.
 * @author The Hive
 */
public class HelpWindow extends javax.swing.JFrame {

    //URL to open
    private String defaultURL;
    
    private URL currentPage = null;
    private URL nextPage = null;
    private URL prevPage = null;
    
    //Listeners
    private HyperlinkListener linkListener = null;
    private ActionListener actionListener = null;
    
    /**
     * Constructor for the HelpWindow class.
     * Initialises various components and sets the page(html(s)) to be displayed
     * to the Window/Frame
     * 
     * @param defaultURL the URL of the index page 
     */
    public HelpWindow(URL url) 
    {
        setFrameIcon(); //set the icon shown on the top left side of the frame

        this.initComponents(); //initialises various components

        /*
         * Set the page to the JEditorPane
         */
        try 
        {
            currentPage = url;
            this.mainEditorPane.setContentType("txt/html");
            this.mainEditorPane.setPage(currentPage);
        } 
        catch (IOException ex) 
        {
            ex.printStackTrace();
        }
    }
    
    /**
     * Overloaded Constructor
     * Uses file absolute path to convert to URL of the html page to be viewed
     * 
     * @param file files absolute path is to be converted to URL
     */
    public HelpWindow(File file) 
    {
        setFrameIcon(); //set the icon shown on the top left side of the frame
         
        this.initComponents(); //initialises various components

        /*
         * Set the page to the JEditorPane
         */
        try 
        {
            currentPage = file.toURI().toURL(); //URL of the html to be displayed
            
            this.mainEditorPane.setContentType("txt/html");
            //this.mainEditorPane.setText(defaultURL);
            
            this.mainEditorPane.setPage(currentPage);
        } 
        catch (IOException ex) 
        {
            ex.printStackTrace();
        }
    }
    
    /**
     * Method to set the next page to be displayed
     * This method is accessed by the controller class to set the next page
     * with the URL that the user clicked on the link.
     * 
     * @param link link from the current page to the next page.
     * @throws IOException exception thrown when the page cannot be set.
     */
    public void viePage(URL link) throws IOException
    {
        this.mainEditorPane.setPage(link);
    }
    
    /**
     * Set the image icon on the window/frame
     */
    public void setFrameIcon()
    {
        URL imageURL = HelpWindow.class.getResource("/resources/images/frameicon.png");
        Image img = Toolkit.getDefaultToolkit().getImage(imageURL);
        
        if(imageURL != null)
        {
            ImageIcon icon = new ImageIcon(img);
            super.setIconImage(icon.getImage());//window icon
        }
    }
    
    //GETTERS AND SETTERS
    public String getDefaultURL() {
        return defaultURL;
    }

    public void setDefaultURL(String defaultURL) {
        this.defaultURL = defaultURL;
    }
    
    public void setActionListeners(ActionListener actionListener)
    {
        this.actionListener = actionListener;
    }
    
    public void setHyperLinkListener(HyperlinkListener linkListener)
    {
        this.linkListener = linkListener;
        this.mainEditorPane.addHyperlinkListener(linkListener);
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        mainEditorPane = new javax.swing.JEditorPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("resources/others/LabelsAndButtonTextBundle"); // NOI18N
        setTitle(bundle.getString("helpWindowTitle")); // NOI18N
        setMinimumSize(new java.awt.Dimension(500, 700));
        setPreferredSize(new java.awt.Dimension(500, 700));
        setResizable(false);

        mainEditorPane.setEditable(false);
        mainEditorPane.setMinimumSize(new java.awt.Dimension(475, 690));
        mainEditorPane.setPreferredSize(new java.awt.Dimension(475, 690));
        jScrollPane1.setViewportView(mainEditorPane);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 492, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 485, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JEditorPane mainEditorPane;
    // End of variables declaration//GEN-END:variables
}
